package com.example.websocketdemo.controller.CIMVUE;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

@RestController
@RequestMapping("/VUEEmergencyRightProductionRisk")
public class EmergencyRightProductionRiskController {
    @GetMapping("/RiskPoints")
    public JSONArray a(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("重大危险源", "18");
        jsonObject.put("关键重大部位", "18");
        jsonObject.put("可燃气体", "810");
        jsonObject.put("环境监测", "0");
        return bianli(jsonObject);
    }

    @GetMapping("/data")
    public JSONArray d(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject.put("nameOfActivity ", "检修作业（承包商）");
        jsonObject.put("WhetherSpecialOperation", "否");
        jsonObject.put("workingArea", "变电站");
        jsonObject.put("time", "2022-06-14 19:48:42");

        jsonObject1.put("nameOfActivity", "巡检作业");
        jsonObject1.put("WhetherSpecialOperation", "否");
        jsonObject1.put("workingArea", "消防泵房");
        jsonObject1.put("time", "2022-06-14 19:49:17");

        jsonObject2.put("nameOfActivity", "设备维保作业（承包商）");
        jsonObject2.put("WhetherSpecialOperation", "否");
        jsonObject2.put("workingArea", "消防泵房");
        jsonObject2.put("time", "2022-06-14 19:49:48");

        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);
        return jsonArray;
    }

    private JSONArray bianli(JSONObject jsonObject){
        JSONArray jsonArray = new JSONArray();
//        JSONObject json = new JSONObject();
        Iterator iter = jsonObject.entrySet().iterator();
        while (iter.hasNext()) {
            JSONObject json = new JSONObject();
            Map.Entry entry = (Map.Entry) iter.next();
            json.put("title",entry.getKey().toString());
            json.put("value",entry.getValue().toString());
            jsonArray.add(json);
        }
        return jsonArray;
    }
}
